<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	
	function logz(){

		$o = D('u');

		$o->where("title = '{$_POST['id']}'");
		$r = $o->find();
		// dump($r);

		if($r==NULL){
			$this->no('找不到用户');
		}
		// echo I('password','','md5');exit;
		if( I('password') != $r['password'] ){
			$this->no('密码不正确');
		}

		//更新登录信息
		$d = array(
			'log_time'	=> time(),
		);
		$o->where("id = '{$r['id']}'");
		$o->save($d);
		// echo $o->_sql();
		// exit;

		//CK
		cookie ( 'id',		$r ['id'] );
		cookie ( 'title',	$r ['title'] );
		cookie ( 'okey',	$r ['okey'] );
		// cookie ( 'groups',	$r ['groups'] );


		$js = "location.href='/v';";
		$this->ok($js);
	}
	function logout() {

		$time = time() - 10;
		cookie ( 'id',		null,	$time );
		cookie ( 'title',	null,	$time );
		cookie ( 'okey',	null,	$time );
		// cookie ( 'groups',	null,	$time );

		header ( "location: /" );

	}
	function fix_user(){

		$o = M('u');
		$r = $o->select();
		foreach($r as $v){

			if(strlen($v['password'])<3){
				$d['password'] = sprintf("%03d", $v['password']);
			}else{
				$d['password'] = $v['password'];
			}

			$d['okey'] = substr( md5($d['password']), 8, 16 );

			$o->where("id = '{$v['id']}'");
			$o->save($d);
		}

		

	}
	function ok($msg){

		$array = array(
			'st'	=> 1,
			'msg'	=> $msg,
		);

		// header("Content-type: text/html; charset=utf-8");
		// echo $msg;
		$this->ajaxReturn($array);
		exit;
	}
	function no($msg){
		
		$array = array(
			'st'	=> 0,
			'msg'	=> $msg,
		);

		// header("Content-type: text/html; charset=utf-8");
		// echo $msg;
		$this->ajaxReturn($array);
		exit;
	}
}